package cn.edu.ccnu.nlp;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;

public class Crawler {
	
	public static Configuration configuration;
	public static HTablePool pool;
	public static HTable news;
	public static HTable blog;
	public static HTable bbs;
	public static HTable news_stat;
	public static HTable blog_stat;
	public static HTable bbs_stat;
	public static HTable news_index;
	public static HTable blog_index;
	public static HTable bbs_index;
	static {
		configuration = HBaseConfiguration.create();
		
		try{
			pool = new HTablePool(configuration, 1000);
			news = (HTable)pool.getTable("news");
			//blog = (HTable)pool.getTable("blog");
			//bbs = (HTable)pool.getTable("bbs");
			news_stat = (HTable)pool.getTable("news_stat");
			//blog_stat = (HTable)pool.getTable("blog_stat");
			//bbs_stat = (HTable)pool.getTable("bbs_stat");
			news_index = (HTable)pool.getTable("news_index");
			//blog_index = (HTable)pool.getTable("blog_index");
			//bbs_index = (HTable)pool.getTable("bbs_index");
		}catch(Exception e){
			e.getStackTrace();
		}
	}
	

	/**
	 * @param args
	 * @throws IOException 
	 */
	public  HashMap<String,Integer> getHourCount(HTable table,String year,String month,String day) throws IOException {
		
		HashMap<String,Integer> map = new HashMap<String,Integer>();
		String time =null,c_hour = null;
	 	String[] hours={"00","01","02","03","04","05","06","07","08","09","10","11","12","13",
	 		"14","15","16","17","18","19","20","21","22","23"};
	 	int[] times = new int[24];                                         //统计次数
	 	ResultScanner rs = null;
	 	HTablePool pool = null;
			try {
				Filter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(year+"-"+month+"-"+day)); 
				Scan s = new Scan();
				s.setFilter(filter);
				rs = table.getScanner(s);
				for (Result r= rs.next();r!=null;r=rs.next()) {
					
					for(KeyValue keyValue:r.raw()){                                         //value值为”time,title“，通过分割获取时间，继而获取小时
								time = (new String(keyValue.getValue())).split(",")[0];
								c_hour = time.split(":")[0];
								if(c_hour.equals("00")){
									times[0]++;
									continue;
								}
								if(c_hour.equals("01")){
									times[1]++;
									continue;
								}
								if(c_hour.equals("02")){
									times[2]++;
									continue;
								}
								if(c_hour.equals("03")){
									times[3]++;
									continue;
								}
								if(c_hour.equals("04")){
									times[4]++;
									continue;
								}
								if(c_hour.equals("05")){
									times[5]++;
									continue;
								}
								if(c_hour.equals("06")){
									times[6]++;
									continue;
								}
								if(c_hour.equals("07")){
									times[7]++;
									continue;
								}
								if(c_hour.equals("08")){
									times[8]++;
									continue;
								}
								if(c_hour.equals("09")){
									times[9]++;
									continue;
								}
								if(c_hour.equals("10")){
									times[10]++;
									continue;
								}
								if(c_hour.equals("11")){
									times[11]++;
									continue;
								}
								if(c_hour.equals("12")){
									times[12]++;
									continue;
								}
								if(c_hour.equals("13")){
									times[13]++;
									continue;
								}
								if(c_hour.equals("14")){
									times[140]++;
									continue;
								}
								if(c_hour.equals("15")){
									times[15]++;
									continue;
								}
								if(c_hour.equals("16")){
									times[16]++;
									continue;
								}
								if(c_hour.equals("17")){
									times[17]++;
									continue;
								}
								if(c_hour.equals("18")){
									times[18]++;
									continue;
								}
								if(c_hour.equals("19")){
									times[19]++;
									continue;
								}
								if(c_hour.equals("20")){
									times[20]++;
									continue;
								}
								if(c_hour.equals("21")){
									times[21]++;
									continue;
								}
								if(c_hour.equals("22")){
									times[22]++;
									continue;
								}
								if(c_hour.equals("23")){
									times[23]++;
									continue;
								}
						}
		         }
					for(int i = 0; i < 24; i++)
						map.put(hours[i], times[i]);                   //存储每个小时及对应的次数并返回

			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				rs.close();
			}
			return map;
}
	
	public String getHourString(int current_year, int current_month, int current_day, int current_hour, String corpus){
		int total_count = 0;
		int hours_count[] = new int[24];
		double hours_score[] = new double[24];

		int max ;

		String c_year,c_month,c_day;
		c_year = ""+current_year;
		if(current_month < 10)
			c_month = "0"+current_month;
		else
			c_month = ""+current_month;
		if(current_day <10)
			c_day = "0"+current_day;
		else
			c_day = ""+current_day;
		try {
				max = 0;
				HashMap<String,Integer> map = new HashMap<String,Integer>();
				if(corpus.equals("news"))
						map= getHourCount(news_index,c_year,c_month,c_day);
				else if(corpus.equals("blog"))
					map= getHourCount(blog_index,c_year,c_month,c_day);
				else if(corpus.equals("bbs"))
						map= getHourCount(bbs_index,c_year,c_month,c_day);
				Set set = map.keySet();
				Iterator it = set.iterator();
				while(it.hasNext()){
					String key = (String)it.next();
					int h = Integer.parseInt(key);		
					int c =hours_count[h] =  (int)map.get(key);		
					max = (c > max) ? c : max;
					total_count += c;
				}
		
				max = (max == 0) ? 100 : max;
				for(int i = 0; i < hours_score.length; i++)
					hours_score[i] = 100 * hours_count[i] / (max * 1.5);
		
				if ( hours_count[current_hour] == 0)
					for(int  i =  hours_count.length - 1; i >= 0; i--)
						if( hours_count[i] > 0 )
						{
							current_hour = i;
							break;
						}

			} catch (Exception e){
				e.printStackTrace();
			}

			String put_time_string = "";

			if( total_count == 0 )
				put_time_string =  "<graph></graph>";
			else
			{
				StringBuffer put_time_xml = new StringBuffer();
				put_time_xml.append("<graph  canvasBorderColor='cccccc'  canvasbgColor='cccccc' caption='' subcaption='' xAxisName='时' yAxisMinValue='0'  decimalPrecision='0' formatNumberScale='0' showNames='1' showValues='0'  showAlternateHGridColor='1' AlternateHGridColor='ff5904' divLineColor='ff5904' divLineAlpha='30' alternateHGridAlpha='5' >");
		
				for(int i = 0; i < 24; i++)
				{
					String link =  String.format("crawler.jsp?c=%s&h=%s", corpus,  i );
					String color = ( i == current_hour ) ? "FF0000" : "ff5904";
					put_time_xml.append( String.format("<set color='%s' name='%s' value='%s'  link='%s'/>",color, i,  hours_count[i] , link) );
				}
				put_time_xml.append("</graph>"); 

				put_time_string = put_time_xml.toString();
		
			}
			System.out.println(put_time_string);
			return put_time_string;
	}
	public static void main(String[] args) throws IOException{
		Crawler craw = new Crawler();
//		HashMap map = craw.getHourCount("news_index", "2005", "01", "01");
////		System.out.println(map1.size());
//		Set set = map.keySet();
//		Iterator it = set.iterator();
//		while(it.hasNext()){
//			String key = (String)it.next();
//			int h = Integer.parseInt(key);		
//			int c  =  (int) map.get(key);
//			System.out.println("key:"+key+":"+map.get(key)+":"+c);
//		}
//		
		craw.getHourString(2005, 1, 1, 8, "news");
	}
}
